package com.kkyy.msystem.controller;

import com.kkyy.msystem.entity.*;
import com.kkyy.msystem.feign.FeignService;
import com.kkyy.msystem.feign.FeignService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/msystem")
public class FindMusicController {
    // 远程调用
    @Autowired
    FeignService feignService;

    @Autowired
    FeignService2 feignService2;

    // 查看收藏(后台)
    @RequestMapping("/myCollection2")
    public String myCollection2(Model model, HttpSession session) {
        // 获取登录用户信息
        UsersEntity user = (UsersEntity) session.getAttribute(UsersEntity.SESSION_USER_KEY);
        // 一定用@RestController
        List<MyMusicEntity> myMusicEntity = feignService2.myCollection2(user.getId());
        model.addAttribute("data", myMusicEntity);
        model.addAttribute("totals", myMusicEntity.size());
        return "mymusic";
    }

    // 收藏删除(后台)
    @RequestMapping("/mymusicdel")
    public String mymusicdel(Integer id) {
        feignService2.mymusicdel(id);
        return "redirect:myCollection2";
    }

    // 查询轮播图(后台)
    @RequestMapping("/bannerslist")
    public String getBanners(Model model) {
        List<BannersEntity> banners2 = feignService.getBanners2();
        List<BannersEntity> list = new ArrayList<>();
        for (BannersEntity banner : banners2) {
            if (banner.getBannersStatus() != 0) {
                list.add(banner);
            }
        }
        model.addAttribute("data", list);
        model.addAttribute("totals", list.size());
        return "banners";
    }

    // 查询歌单(后台)
    @RequestMapping("/playlistslist")
    public String getPlaylists(Model model) {
        List<PlayListsEntity> playLists2 = feignService.getPlayLists2();
        List<PlayListsEntity> list = new ArrayList<>();
        for (PlayListsEntity playList : playLists2) {
            if (playList.getPlaylistsStatus() != 0) {
                list.add(playList);
            }
        }
        model.addAttribute("data", list);
        model.addAttribute("totals", list.size());
        return "playlists";
    }

    // 查询音乐(后台)
    @RequestMapping("/songslist")
    public String getSongs(Model model) {
        List<SongsEntity> songs2 = feignService.getSongs2();
        List<SongsEntity> list = new ArrayList<>();
        for (SongsEntity song : songs2) {
            if (song.getSongsStatus() != 0) {
                list.add(song);
            }
        }
        model.addAttribute("data", list);
        model.addAttribute("totals", list.size());
        return "songs";
    }

    // 查询mv(后台)
    @RequestMapping("/mvslist")
    public String getMvs(Model model) {
        List<MvsEntity> mvs2 = feignService.getMvs2();
        List<MvsEntity> list = new ArrayList<>();
        for (MvsEntity mv : mvs2) {
            if (mv.getMvsStatus() != 0) {
                list.add(mv);
            }
        }
        model.addAttribute("data", list);
        model.addAttribute("totals", list.size());
        return "mvs";
    }

    // 删除轮播图(假删)
    @RequestMapping("/bannerdel")
    public String bannerDelById(Integer id) {
        // 远程调用传参带@RequestParam注解
        feignService.delBannerById(id);
        return "redirect:bannerslist";
    }

    // 删除推荐歌单
    @RequestMapping("/playlistdel")
    public String playListDelById(Integer id) {
        feignService.delPlayListById(id);
        return "redirect:playlistslist";
    }

    // 删除最新音乐
    @RequestMapping("/songdel")
    public String songDelById(Integer id) {
        feignService.delSongById(id);
        return "redirect:songslist";
    }

    // 删除mv
    @RequestMapping("/mvdel")
    public String mvDelById(Integer id) {
        feignService.delMvById(id);
        return "redirect:mvslist";
    }

    // 上架mv
    @RequestMapping("/upMv")
    @ResponseBody
    public String upMv(Integer id) {
        feignService.upMvById(id);
        return "成功上架！";
    }

    // 下架mv
    @RequestMapping("/downMv")
    @ResponseBody
    public String downMv(Integer id) {
        feignService.downMvById(id);
        return "成功下架！";
    }

    @RequestMapping("/upPlaylists")
    @ResponseBody
    public String upPlaylists(Integer id) {
        feignService.upPlaylistsById(id);
        return "成功上架！";
    }

    @RequestMapping("/downPlaylists")
    @ResponseBody
    public String downPlaylists(Integer id) {
        feignService.downPlaylistsById(id);
        return "成功下架！";
    }

    @RequestMapping("/upSong")
    @ResponseBody
    public String upSongs(Integer id) {
        feignService.upSongById(id);
        return "成功上架！";
    }

    @RequestMapping("/downSong")
    @ResponseBody
    public String downSongs(Integer id) {
        feignService.downSongById(id);
        return "成功下架！";
    }
}